<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=657938
-->
<head>
  <title>Test for <meter></title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=657938">Mozilla Bug 657938</a>
<p id="display"></p>
<iframe name="submit_frame" style="visibility: hidden;"></iframe>
<div id="content" style="visibility: hidden;">
  <form id='f' method='get' target='submit_frame' action='foo'>
    <meter id='m' value=0.5></meter>
  </form>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for <meter> **/

function checkFormIDLAttribute(aElement)
{
  is('form' in aElement, false, "<meter> shouldn't have a form attribute");
}

function checkAttribute(aElement, aAttribute, aNewValue, aExpectedValueForIDL)
{
  var expectedValueForIDL = aNewValue;
  var expectedValueForContent = String(aNewValue);

  if (aExpectedValueForIDL !== undefined) {
    expectedValueForIDL = aExpectedValueForIDL;
  }

  if (aNewValue != null) {
    aElement.setAttribute(aAttribute, aNewValue);
    is(aElement.getAttribute(aAttribute), expectedValueForContent,
       aAttribute + " content attribute should be " + expectedValueForContent);
    is(aElement[aAttribute], expectedValueForIDL,
       aAttribute + " IDL attribute should be " + expectedValueForIDL);

    if (parseFloat(aNewValue) == aNewValue) {
      aElement[aAttribute] = aNewValue;
      is(aElement.getAttribute(aAttribute), expectedValueForContent,
         aAttribute + " content attribute should be " + expectedValueForContent);
      is(aElement[aAttribute], parseFloat(expectedValueForIDL),
         aAttribute + " IDL attribute should be " + parseFloat(expectedValueForIDL));
    }
  } else {
    aElement.removeAttribute(aAttribute);
    is(aElement.getAttribute(aAttribute), null,
       aAttribute + " content attribute should be null");
    is(aElement[aAttribute], expectedValueForIDL,
       aAttribute + " IDL attribute should be " + expectedValueForIDL);
  }
}

function checkValueAttribute()
{
  var tests = [
    // value has to be a valid float, its default value is 0.0 otherwise.
    [ null, 0.0 ],
    [ 'foo', 0.0 ],
    // If value < 0.0, 0.0 is used instead.
    [ -1.0, 0.0 ],
    // If value >= max, max is used instead (max default value is 1.0).
    [  2.0, 1.0 ],
    [  1.0, 0.5, 0.5 ],
    [  10.0, 5.0, 5.0 ],
    [ 13.37, 13.37, 42.0 ],
    // If value <= min, min is used instead (min default value is 0.0).
    [  0.5, 1.0, 10.0 ,1.0 ],
    [  10.0, 13.37, 42.0 , 13.37],
    // Regular reflection.
    [  0.0 ],
    [  0.5 ],
    [  1.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    if (test[2]) {
      element.setAttribute('max', test[2]);
    }

    if (test[3]) {
      element.setAttribute('min', test[3]);
    }

    checkAttribute(element, 'value', test[0], test[1]);

    element.removeAttribute('max');
    element.removeAttribute('min');
  }
}

function checkMinAttribute()
{
  var tests = [
    // min default value is 0.0.
    [ null, 0.0 ],
    [ 'foo', 0.0 ],
    // Regular reflection.
    [  0.5 ],
    [  1.0 ],
    [  2.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    checkAttribute(element, 'min', test[0], test[1]);
  }
}

function checkMaxAttribute()
{
  var tests = [
    // max default value is 1.0.
    [ null, 1.0 ],
    [ 'foo', 1.0 ],
    // If value <= min, min is used instead.
    [ -1.0, 0.0 ],
    [ 0.0, 0.5, 0.5 ],
    [ 10.0, 15.0, 15.0 ],
    [ 42, 42, 13.37 ],
    // Regular reflection.
    [  0.5 ],
    [  1.0 ],
    [  2.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    if (test[2]) {
      element.setAttribute('min', test[2]);
    }

    checkAttribute(element, 'max', test[0], test[1]);

    element.removeAttribute('min');
  }
}

function checkLowAttribute()
{
  var tests = [
    // low default value is min (min default value is 0.0).
    [ null, 0.0 ],
    [ 'foo', 0.0 ],
    [ 'foo', 1.0, 1.0],
    // If low <= min, min is used instead.
    [ -1.0, 0.0 ],
    [ 0.0, 0.5, 0.5 ],
    [ 10.0, 15.0, 15.0, 42.0 ],
    [ 42.0, 42.0, 13.37, 100.0 ],
    // If low >= max, max is used instead.
    [ 2.0, 1.0 ],
    [ 10.0, 5.0 , 0.5, 5.0 ],
    [ 13.37, 13.37, 0.0, 42.0 ],
    // Regular reflection.
    [  0.0 ],
    [  0.5 ],
    [  1.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    if (test[2]) {
      element.setAttribute('min', test[2]);
    }
    if (test[3]) {
      element.setAttribute('max', test[3]);
    }

    checkAttribute(element, 'low', test[0], test[1]);

    element.removeAttribute('min');
    element.removeAttribute('max');
  }
}

function checkHighAttribute()
{
  var tests = [
    // high default value is max (max default value is 1.0).
    [ null, 1.0 ],
    [ 'foo', 1.0 ],
    [ 'foo', 42.0, 0.0, 42.0],
    // If high <= min, min is used instead.
    [ -1.0, 0.0 ],
    [ 0.0, 0.5, 0.5 ],
    [ 10.0, 15.0, 15.0, 42.0 ],
    [ 42.0, 42.0, 13.37, 100.0 ],
    // If high >= max, max is used instead.
    [ 2.0, 1.0 ],
    [ 10.0, 5.0 , 0.5, 5.0 ],
    [ 13.37, 13.37, 0.0, 42.0 ],
    // Regular reflection.
    [  0.0 ],
    [  0.5 ],
    [  1.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    if (test[2]) {
      element.setAttribute('min', test[2]);
    }
    if (test[3]) {
      element.setAttribute('max', test[3]);
    }

    checkAttribute(element, 'high', test[0], test[1]);

    element.removeAttribute('min');
    element.removeAttribute('max');
  }
}

function checkOptimumAttribute()
{
  var tests = [
    // opt default value is (max-min)/2 (thus default value is 0.5).
    [ null, 0.5 ],
    [ 'foo', 0.5 ],
    [ 'foo', 2.0, 1.0, 3.0],
    // If opt <= min, min is used instead.
    [ -1.0, 0.0 ],
    [ 0.0, 0.5, 0.5 ],
    [ 10.0, 15.0, 15.0, 42.0 ],
    [ 42.0, 42.0, 13.37, 100.0 ],
    // If opt >= max, max is used instead.
    [ 2.0, 1.0 ],
    [ 10.0, 5.0 , 0.5, 5.0 ],
    [ 13.37, 13.37, 0.0, 42.0 ],
    // Regular reflection.
    [  0.0 ],
    [  0.5 ],
    [  1.0 ],
    // Check double-precision value.
    [  0.234567898765432 ],
  ];

  var element = document.createElement('meter');

  for (var test of tests) {
    if (test[2]) {
      element.setAttribute('min', test[2]);
    }
    if (test[3]) {
      element.setAttribute('max', test[3]);
    }

    checkAttribute(element, 'optimum', test[0], test[1]);

    element.removeAttribute('min');
    element.removeAttribute('max');
  }
}

function checkFormListedElement(aElement)
{
  is(document.forms[0].elements.length, 0, "the form should have no element");
}

function checkLabelable(aElement)
{
  var content = document.getElementById('content');
  var label = document.createElement('label');

  content.appendChild(label);
  label.appendChild(aElement);
  is(label.control, aElement, "meter should be labelable");

  // Cleaning-up.
  content.removeChild(label);
  content.appendChild(aElement);
}

function checkNotResetableAndFormSubmission(aElement)
{
  // Creating an input element to check the submission worked.
  var form = document.forms[0];
  var input = document.createElement('input');

  input.name = 'a';
  input.value = 'tulip';
  form.appendChild(input);

  // Setting values.
  aElement.value = 42.0;
  aElement.max = 100.0;

  document.getElementsByName('submit_frame')[0].addEventListener("load", function() {
    document.getElementsByName('submit_frame')[0].removeEventListener("load", arguments.callee, false);

    /**
     * All elements values have been set just before the submission.
     * The input element value should be in the submit url but the meter
     * element value should not appear.
     */
    is(frames['submit_frame'].location.href,
      'http://mochi.test:8888/tests/dom/html/test/forms/foo?a=tulip',
       "The meter element value should not be submitted");

    checkNotResetable();
  }, false);

  form.submit();
}

function checkNotResetable()
{
  // Try to reset the form.
  var form = document.forms[0];
  var element = document.getElementById('m');

  element.value = 3.0;
  element.max = 42.0;

  form.reset();

  SimpleTest.executeSoon(function() {
    is(element.value, 3.0, "meter.value should not have changed");
    is(element.max, 42.0, "meter.max should not have changed");

    SimpleTest.finish();
  });
}

SimpleTest.waitForExplicitFinish();

var m = document.getElementById('m');

ok(m instanceof HTMLMeterElement,
   "The meter element should be instance of HTMLMeterElement");
is(m.constructor, HTMLMeterElement,
   "The meter element constructor should be HTMLMeterElement");

// There is no such attribute.
checkFormIDLAttribute(m);

checkValueAttribute();

checkMinAttribute();

checkMaxAttribute();

checkLowAttribute();

checkHighAttribute();

checkOptimumAttribute();

checkFormListedElement(m);

checkLabelable(m);

checkNotResetableAndFormSubmission(m);

</script>
</pre>
</body>
</html>
